package rs.webnet.android.kamere;

import rs.webnet.android.kamere.data.DataFetcher;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class KamereActivity extends Activity {
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.homepage_layout);

		Button startCamerasMap_btn = (Button) findViewById(R.id.policeCameras);
		Button startReportsMap_btn = (Button) findViewById(R.id.amssReport);
		Button startTickets_btn = (Button) findViewById(R.id.tickets);
		Button startOsmatrac_btn = (Button) findViewById(R.id.osmatrac);

		startCamerasMap_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (DataFetcher.cameras != null) {
					DataFetcher.initCameras(getApplicationContext());
					Intent intent = new Intent(getApplicationContext(), CamerasMapActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "Podaci nisu dostupni!", Toast.LENGTH_LONG).show();
				}
			}
		});
		startReportsMap_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (DataFetcher.roadReport != null) {
					Log.d("d","############## "+DataFetcher.roadReport.size());
					DataFetcher.initReport(getApplicationContext());
					Intent intent = new Intent(getApplicationContext(), ReportsMapActivity.class);
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "Podaci nisu dostupni!", Toast.LENGTH_LONG).show();
				}
			}
		});
		startTickets_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), TicketsActivity.class);
				startActivity(intent);
			}
		});
		startOsmatrac_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_MAIN);
				PackageManager manager = getPackageManager();
				if (manager.getLaunchIntentForPackage("rs.ailic.osmatrac") == null) {
					Toast.makeText(getApplicationContext(), "Preuzmite aplikaciju Osmatrač sa marketa!", Toast.LENGTH_LONG).show();
					Intent intent = new Intent(Intent.ACTION_VIEW);
					intent.setData(Uri.parse("market://details?id=rs.ailic.osmatrac"));
					startActivity(intent);
				} else {
					Toast.makeText(getApplicationContext(), "Startuje se aplikacija Osmatrač", Toast.LENGTH_LONG).show();
					i = manager.getLaunchIntentForPackage("rs.ailic.osmatrac");
					i.addCategory(Intent.CATEGORY_LAUNCHER);
					startActivity(i);
				}
			}
		});
	}

}